/*
 * FileHandler.java
 *
 * Created on 2007��10��24��, ����4:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.afaker.searth.core.index.handler;

import com.afaker.searth.core.search.Searcher;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;

/**
 *
 * @author new
 */
public class FileHandler extends  DocumentHandler{
    public static final Integer PDF = 0;
    public static final Integer TEXT = 1;
    public static final Integer HTML = 2;
    public static final Integer DOC = 3;
    public static HashMap formatMap = new HashMap();
    static {
        formatMap.put("pdf",PDF);
        formatMap.put("txt",TEXT);
        formatMap.put("htm",HTML);
        formatMap.put("html",HTML);
        formatMap.put("doc",DOC);
    }
    private Properties p;
    private final String classExtFile = "global.properties";
    private Searcher searcher;
    private boolean indexPlainText = false;
    private Set stopFormat = new HashSet();
    /** Creates a new instance of FileHandler */
    public FileHandler() {
        p = new Properties();
        try {
            p.load(new FileInputStream(classExtFile));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        searcher = new Searcher();
    }
    public void addStopformat(int format){
        if(format > 3 || format < 0)
            return ;
        stopFormat.add(Integer.valueOf(format));
    }
    public void setIndexPlainText(boolean indexPlainText) {
        this.indexPlainText = indexPlainText;
    }
    public void clearStopFormat(){
        stopFormat.clear();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        FileHandler h = new FileHandler();
        Document doc = h.getDocument(new File("E:/ip.txt"));
        System.out.println(doc.get("path"));
    }
    
    public Document getDocument(File file) {
        String path = file.getAbsolutePath();
        int dotIndex = path.lastIndexOf(".");
        String extention;
        if((dotIndex < path.length()) && (dotIndex > 0)) {
            extention = path.substring(dotIndex + 1);
            extention = extention.toLowerCase();
            if(stopFormat.contains(formatMap.get(extention))){
                return null;
            }
            String handlerClassName = p.getProperty(extention);
            if(handlerClassName == null || handlerClassName.length() < 0) {
                if(indexPlainText){
                   // extention = "all";
                    handlerClassName = "com.bluesteng.searth.index.handler.PlainTextHandler";
                } else{
                    log.warn("File format: " + extention + " is not supported");
                    return null;
                }
            }
            //If the file has been indexed,ignored it.
            searcher.search("\"" + path + "\"","path");
            Hits hits = searcher.getHits();
            if(hits != null){
                if(hits.length() > 0) {
                    log.info("File has been indexed.");
                    return null;
                }
            }
            
            Class handlerClass;
            try {
                handlerClass = Class.forName(handlerClassName);
                DocumentHandler handler = null;
                try {
                    handler = (DocumentHandler) handlerClass.newInstance();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                }
                return handler.getDocument(file);
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }            
        }
      
        return null;
    }
    
}
